home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 242 / Issue 242 - April 2008 - DPCS0408DVD.ISO / Open Source / AutoHotKey / Source / AutoHotkey104705_source.exe / source / AutoHotkey.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2007-11-20  |  17.7 KB  |  324 lines

  1. /*
  2. AutoHotkey
  3.  
  4. Copyright 2003-2006 Chris Mallett (support@autohotkey.com)
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15. */
  16.  
  17. #include "stdafx.h" // pre-compiled headers
  18. #include "globaldata.h" // for access to many global vars
  19. #include "application.h" // for MsgSleep()
  20. #include "window.h" // For MsgBox() & SetForegroundLockTimeout()
  21.  
  22. // General note:
  23. // The use of Sleep() should be avoided *anywhere* in the code.  Instead, call MsgSleep().
  24. // The reason for this is that if the keyboard or mouse hook is installed, a straight call
  25. // to Sleep() will cause user keystrokes & mouse events to lag because the message pump
  26. // (GetMessage() or PeekMessage()) is the only means by which events are ever sent to the
  27. // hook functions.
  28.  
  29.  
  30. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  31. {
  32.     // Init any globals not in "struct g" that need it:
  33.     g_hInstance = hInstance;
  34.     InitializeCriticalSection(&g_CriticalRegExCache); // v1.0.45.04: Must be done early so that it's unconditional, so that DeleteCriticalSection() in the script destructor can also be unconditional (deleting when never initialized can crash, at least on Win 9x).
  35.  
  36.     if (!GetCurrentDirectory(sizeof(g_WorkingDir), g_WorkingDir)) // Needed for the FileSelectFile() workaround.
  37.         *g_WorkingDir = '\0';
  38.     // Unlike the below, the above must not be Malloc'd because the contents can later change to something
  39.     // as large as MAX_PATH by means of the SetWorkingDir command.
  40.     g_WorkingDirOrig = SimpleHeap::Malloc(g_WorkingDir); // Needed by the Reload command.
  41.  
  42.     // Set defaults, to be overridden by command line args we receive:
  43.     bool restart_mode = false;
  44.  
  45. #ifndef AUTOHOTKEYSC
  46.     #ifdef _DEBUG
  47.         //char *script_filespec = "C:\\Util\\AutoHotkey.ahk";
  48.         //char *script_filespec = "C:\\A-Source\\AutoHotkey\\Test\\GUI Demo.ahk";
  49.         //char *script_filespec = "C:\\A-Source\\AutoHotkey\\Test\\TEST SUITES\\MAIN.ahk";
  50.         //char *script_filespec = "C:\\A-Source\\AutoHotkey\\Test\\TEST SUITES\\Expressions.ahk";
  51.         //char *script_filespec = "C:\\A-Source\\AutoHotkey\\Test\\TEST SUITES\\Line Continuation.ahk";
  52.         //char *script_filespec = "C:\\A-Source\\AutoHotkey\\Test\\TEST SUITES\\DllCall.ahk";
  53.         char *script_filespec = "C:\\A-Source\\AutoHotkey\\Test\\TEST SUITES\\RegExMatch & RegExReplace.ahk";
  54.         //char *script_filespec = "C:\\A-Source\\AutoHotkey\\Test\\TEST SUITES\\Win commands, all cases.ahk";
  55.         //char *script_filespec = "C:\\A-Source\\AutoHotkey\\Test\\TEST SUITES\\GUI Date.ahk";
  56.         //char *script_filespec = "C:\\A-Source\\AutoHotkey\\Test\\TEST SUITES\\GUI ListView.ahk";
  57.         //char *script_filespec = "C:\\A-Source\\AutoHotkey\\Test\\TEST SUITES\\OnMessage.ahk";
  58.         //char *script_filespec = "C:\\A-Source\\AutoHotkey\\Test\\TEST SUITES\\Send command.ahk";
  59.         //char *script_filespec = "C:\\A-Source\\AutoHotkey\\Ref\\ImageSearch\\TEST SUITE\\MAIN.ahk";
  60.         //char *script_filespec = "C:\\A-Source\\AutoHotkey\\Test\\New Text Document.ahk";
  61.     #else
  62.         char *script_filespec = NULL; // Set default as "unspecified/omitted".
  63.     #endif
  64. #endif
  65.  
  66.     // The problem of some command line parameters such as /r being "reserved" is a design flaw (one that
  67.     // can't be fixed without breaking existing scripts).  Fortunately, I think it affects only compiled
  68.     // scripts because running a script via AutoHotkey.exe should avoid treating anything after the
  69.     // filename as switches. This flaw probably occurred because when this part of the program was designed,
  70.     // there was no plan to have compiled scripts.
  71.     // 
  72.     // Examine command line args.  Rules:
  73.     // Any special flags (e.g. /force and /restart) must appear prior to the script filespec.
  74.     // The script filespec (if present) must be the first non-backslash arg.
  75.     // All args that appear after the filespec are considered to be parameters for the script
  76.     // and will be added as variables %1% %2% etc.
  77.     // The above rules effectively make it impossible to autostart AutoHotkey.ini with parameters
  78.     // unless the filename is explicitly given (shouldn't be an issue for 99.9% of people).
  79.     char var_name[32], *param; // Small size since only numbers will be used (e.g. %1%, %2%).
  80.     Var *var;
  81.     bool switch_processing_is_complete = false;
  82.     int script_param_num = 1;
  83.  
  84.     for (int i = 1; i < __argc; ++i) // Start at 1 because 0 contains the program name.
  85.     {
  86.         param = __argv[i]; // For performance and convenience.
  87.         if (switch_processing_is_complete) // All args are now considered to be input parameters for the script.
  88.         {
  89.             if (   !(var = g_script.FindOrAddVar(var_name, sprintf(var_name, "%d", script_param_num)))   )
  90.                 return CRITICAL_ERROR;  // Realistically should never happen.
  91.             var->Assign(param);
  92.             ++script_param_num;
  93.         }
  94.         // Insist that switches be an exact match for the allowed values to cut down on ambiguity.
  95.         // For example, if the user runs "CompiledScript.exe /find", we want /find to be considered
  96.         // an input parameter for the script rather than a switch:
  97.         else if (!stricmp(param, "/R") || !stricmp(param, "/restart"))
  98.             restart_mode = true;
  99.         else if (!stricmp(param, "/F") || !stricmp(param, "/force"))
  100.             g_ForceLaunch = true;
  101.         else if (!stricmp(param, "/ErrorStdOut"))
  102.             g_script.mErrorStdOut = true;
  103. #ifndef AUTOHOTKEYSC // i.e. the following switch is recognized only by AutoHotkey.exe (especially since recognizing new switches in compiled scripts can break them, unlike AutoHotkey.exe).
  104.         else if (!stricmp(param, "/iLib")) // v1.0.47: Build an include-file so that ahk2exe can include library functions called by the script.
  105.         {
  106.             ++i; // Consume the next parameter too, because it's associated with this one.
  107.             if (i >= __argc) // Missing the expected filename parameter.
  108.                 return CRITICAL_ERROR;
  109.             // For performance and simplicity, open/crease the file unconditionally and keep it open until exit.
  110.             if (   !(g_script.mIncludeLibraryFunctionsThenExit = fopen(__argv[i], "w"))   ) // Can't open the temp file.
  111.                 return CRITICAL_ERROR;
  112.         }
  113. #endif
  114.         else // since this is not a recognized switch, the end of the [Switches] section has been reached (by design).
  115.         {
  116.             switch_processing_is_complete = true;  // No more switches allowed after this point.
  117. #ifdef AUTOHOTKEYSC
  118.             --i; // Make the loop process this item again so that it will be treated as a script param.
  119. #else
  120.             script_filespec = param;  // The first unrecognized switch must be the script filespec, by design.
  121. #endif
  122.         }
  123.     }
  124.  
  125. #ifndef AUTOHOTKEYSC
  126.     if (script_filespec)// Script filename was explicitly specified, so check if it has the special conversion flag.
  127.     {
  128.         size_t filespec_length = strlen(script_filespec);
  129.         if (filespec_length >= CONVERSION_FLAG_LENGTH)
  130.         {
  131.             char *cp = script_filespec + filespec_length - CONVERSION_FLAG_LENGTH;
  132.             // Now cp points to the first dot in the CONVERSION_FLAG of script_filespec (if it has one).
  133.             if (!stricmp(cp, CONVERSION_FLAG))
  134.                 return Line::ConvertEscapeChar(script_filespec);
  135.         }
  136.     }
  137. #endif
  138.  
  139.     // Like AutoIt2, store the number of script parameters in the script variable %0%, even if it's zero:
  140.     if (   !(var = g_script.FindOrAddVar("0"))   )
  141.         return CRITICAL_ERROR;  // Realistically should never happen.
  142.     var->Assign(script_param_num - 1);
  143.  
  144.     global_init(g);  // Set defaults prior to the below, since below might override them for AutoIt2 scripts.
  145.  
  146. // Set up the basics of the script:
  147. #ifdef AUTOHOTKEYSC
  148.     if (g_script.Init("", restart_mode) != OK) 
  149. #else
  150.     if (g_script.Init(script_filespec, restart_mode) != OK)  // Set up the basics of the script, using the above.
  151. #endif
  152.         return CRITICAL_ERROR;
  153.  
  154.     // Set g_default now, reflecting any changes made to "g" above, in case AutoExecSection(), below,
  155.     // never returns, perhaps because it contains an infinite loop (intentional or not):
  156.     CopyMemory(&g_default, &g, sizeof(global_struct));
  157.  
  158.     // Could use CreateMutex() but that seems pointless because we have to discover the
  159.     // hWnd of the existing process so that we can close or restart it, so we would have
  160.     // to do this check anyway, which serves both purposes.  Alt method is this:
  161.     // Even if a 2nd instance is run with the /force switch and then a 3rd instance
  162.     // is run without it, that 3rd instance should still be blocked because the
  163.     // second created a 2nd handle to the mutex that won't be closed until the 2nd
  164.     // instance terminates, so it should work ok:
  165.     //CreateMutex(NULL, FALSE, script_filespec); // script_filespec seems a good choice for uniqueness.
  166.     //if (!g_ForceLaunch && !restart_mode && GetLastError() == ERROR_ALREADY_EXISTS)
  167.  
  168. #ifdef AUTOHOTKEYSC
  169.     LineNumberType load_result = g_script.LoadFromFile();
  170. #else
  171.     LineNumberType load_result = g_script.LoadFromFile(script_filespec == NULL);
  172. #endif
  173.     if (load_result == LOADING_FAILED) // Error during load (was already displayed by the function call).
  174.         return CRITICAL_ERROR;  // Should return this value because PostQuitMessage() also uses it.
  175.     if (!load_result) // LoadFromFile() relies upon us to do this check.  No lines were loaded, so we're done.
  176.         return 0;
  177.  
  178.     // Unless explicitly set to be non-SingleInstance via SINGLE_INSTANCE_OFF or a special kind of
  179.     // SingleInstance such as SINGLE_INSTANCE_REPLACE and SINGLE_INSTANCE_IGNORE, persistent scripts
  180.     // and those that contain hotkeys/hotstrings are automatically SINGLE_INSTANCE_PROMPT as of v1.0.16:
  181.     if (g_AllowOnlyOneInstance == ALLOW_MULTI_INSTANCE && IS_PERSISTENT)
  182.         g_AllowOnlyOneInstance = SINGLE_INSTANCE_PROMPT;
  183.  
  184.     HWND w_existing = NULL;
  185.     UserMessages reason_to_close_prior = (UserMessages)0;
  186.     if (g_AllowOnlyOneInstance && g_AllowOnlyOneInstance != SINGLE_INSTANCE_OFF && !restart_mode && !g_ForceLaunch)
  187.     {
  188.         // Note: the title below must be constructed the same was as is done by our
  189.         // CreateWindows(), which is why it's standardized in g_script.mMainWindowTitle:
  190.         if (w_existing = FindWindow(WINDOW_CLASS_MAIN, g_script.mMainWindowTitle))
  191.         {
  192.             if (g_AllowOnlyOneInstance == SINGLE_INSTANCE_IGNORE)
  193.                 return 0;
  194.             if (g_AllowOnlyOneInstance != SINGLE_INSTANCE_REPLACE)
  195.                 if (MsgBox("An older instance of this script is already running.  Replace it with this"
  196.                     " instance?\nNote: To avoid this message, see #SingleInstance in the help file."
  197.                     , MB_YESNO, g_script.mFileName) == IDNO)
  198.                     return 0;
  199.             // Otherwise:
  200.             reason_to_close_prior = AHK_EXIT_BY_SINGLEINSTANCE;
  201.         }
  202.     }
  203.     if (!reason_to_close_prior && restart_mode)
  204.         if (w_existing = FindWindow(WINDOW_CLASS_MAIN, g_script.mMainWindowTitle))
  205.             reason_to_close_prior = AHK_EXIT_BY_RELOAD;
  206.     if (reason_to_close_prior)
  207.     {
  208.         // Now that the script has been validated and is ready to run, close the prior instance.
  209.         // We wait until now to do this so that the prior instance's "restart" hotkey will still
  210.         // be available to use again after the user has fixed the script.  UPDATE: We now inform
  211.         // the prior instance of why it is being asked to close so that it can make that reason
  212.         // available to the OnExit subroutine via a built-in variable:
  213.         ASK_INSTANCE_TO_CLOSE(w_existing, reason_to_close_prior);
  214.         //PostMessage(w_existing, WM_CLOSE, 0, 0);
  215.  
  216.         // Wait for it to close before we continue, so that it will deinstall any
  217.         // hooks and unregister any hotkeys it has:
  218.         int interval_count;
  219.         for (interval_count = 0; ; ++interval_count)
  220.         {
  221.             Sleep(20);  // No need to use MsgSleep() in this case.
  222.             if (!IsWindow(w_existing))
  223.                 break;  // done waiting.
  224.             if (interval_count == 100)
  225.             {
  226.                 // This can happen if the previous instance has an OnExit subroutine that takes a long
  227.                 // time to finish, or if it's waiting for a network drive to timeout or some other
  228.                 // operation in which it's thread is occupied.
  229.                 if (MsgBox("Could not close the previous instance of this script.  Keep waiting?", 4) == IDNO)
  230.                     return CRITICAL_ERROR;
  231.                 interval_count = 0;
  232.             }
  233.         }
  234.         // Give it a small amount of additional time to completely terminate, even though
  235.         // its main window has already been destroyed:
  236.         Sleep(100);
  237.     }
  238.  
  239.     // Call this only after closing any existing instance of the program,
  240.     // because otherwise the change to the "focus stealing" setting would never be undone:
  241.     SetForegroundLockTimeout();
  242.  
  243.     // Create all our windows and the tray icon.  This is done after all other chances
  244.     // to return early due to an error have passed, above.
  245.     if (g_script.CreateWindows() != OK)
  246.         return CRITICAL_ERROR;
  247.  
  248.     // At this point, it is nearly certain that the script will be executed.
  249.  
  250.     if (g_MaxHistoryKeys && (g_KeyHistory = (KeyHistoryItem *)malloc(g_MaxHistoryKeys * sizeof(KeyHistoryItem))))
  251.         ZeroMemory(g_KeyHistory, g_MaxHistoryKeys * sizeof(KeyHistoryItem)); // Must be zeroed.
  252.     //else leave it NULL as it was initialized in globaldata.
  253.  
  254.     // MSDN: "Windows XP: If a manifest is used, InitCommonControlsEx is not required."
  255.     // Therefore, in case it's a high overhead call, it's not done on XP or later:
  256.     if (!g_os.IsWinXPorLater())
  257.     {
  258.         // Since InitCommonControls() is apparently incapable of initializing DateTime and MonthCal
  259.         // controls, InitCommonControlsEx() must be called.  But since Ex() requires comctl32.dll
  260.         // 4.70+, must get the function's address dynamically in case the program is running on
  261.         // Windows 95/NT without the updated DLL (otherwise the program would not launch at all).
  262.         typedef BOOL (WINAPI *MyInitCommonControlsExType)(LPINITCOMMONCONTROLSEX);
  263.         MyInitCommonControlsExType MyInitCommonControlsEx = (MyInitCommonControlsExType)
  264.             GetProcAddress(GetModuleHandle("comctl32"), "InitCommonControlsEx"); // LoadLibrary shouldn't be necessary because comctl32 in linked by compiler.
  265.         if (MyInitCommonControlsEx)
  266.         {
  267.             INITCOMMONCONTROLSEX icce;
  268.             icce.dwSize = sizeof(INITCOMMONCONTROLSEX);
  269.             icce.dwICC = ICC_WIN95_CLASSES | ICC_DATE_CLASSES; // ICC_WIN95_CLASSES is equivalent to calling InitCommonControls().
  270.             MyInitCommonControlsEx(&icce);
  271.         }
  272.         else // InitCommonControlsEx not available, so must revert to non-Ex() to make controls work on Win95/NT4.
  273.             InitCommonControls();
  274.     }
  275.  
  276.     // Activate the hotkeys, hotstrings, and any hooks that are required prior to executing the
  277.     // top part (the auto-execute part) of the script so that they will be in effect even if the
  278.     // top part is something that's very involved and requires user interaction:
  279.     Hotkey::ManifestAllHotkeysHotstringsHooks(); // We want these active now in case auto-execute never returns (e.g. loop)
  280.     g_script.mIsReadyToExecute = true; // This is done only now for error reporting purposes in Hotkey.cpp.
  281.  
  282.     // Run the auto-execute part at the top of the script (this call might never return):
  283.     ResultType result = g_script.AutoExecSection();
  284.     // If no hotkeys are in effect, the user hasn't requested a hook to be activated, and the script
  285.     // doesn't contain the #Persistent directive we're done unless the OnExit subroutine doesn't exit:
  286.     if (!IS_PERSISTENT) // Resolve macro again in case any of its components changed since the last time.
  287.         g_script.ExitApp(result == FAIL ? EXIT_ERROR : EXIT_EXIT);
  288.  
  289.     // The below is done even if AutoExecSectionTimeout() already set the values once.
  290.     // This is because when the AutoExecute section finally does finish, by definition it's
  291.     // supposed to store the global settings that are currently in effect as the default values.
  292.     // In other words, the only purpose of AutoExecSectionTimeout() is to handle cases where
  293.     // the AutoExecute section takes a long time to complete, or never completes (perhaps because
  294.     // it is being used by the script as a "backround thread" of sorts):
  295.     // Save the values of KeyDelay, WinDelay etc. in case they were changed by the auto-execute part
  296.     // of the script.  These new defaults will be put into effect whenever a new hotkey subroutine
  297.     // is launched.  Each launched subroutine may then change the values for its own purposes without
  298.     // affecting the settings for other subroutines:
  299.     global_clear_state(g);  // Start with a "clean slate" in both g and g_default.
  300.     CopyMemory(&g_default, &g, sizeof(global_struct)); // Above has set g.IsPaused==false in case it's ever possible that it's true as a result of AutoExecSection().
  301.     // After this point, the values in g_default should never be changed.
  302.  
  303.     // It seems best to set ErrorLevel to NONE after the auto-execute part of the script is done.
  304.     // However, we do not set it to NONE right before launching each new hotkey subroutine because
  305.     // it's more flexible that way (i.e. the user may want one hotkey subroutine to use the value of
  306.     // ErrorLevel set by another).  This reset was also done by LoadFromFile(), but we do it again
  307.     // here in case the auto-exectute section changed it:
  308.     g_ErrorLevel->Assign(ERRORLEVEL_NONE);
  309.  
  310.     // Since we're about to enter the script's idle state, set the "idle thread" to
  311.     // be minimum priority so that it can always be "interrupted" (though technically,
  312.     // there is no actual idle quasi-thread, so it can't really be interrupted):
  313.     g.Priority = PRIORITY_MINIMUM;
  314.     g.ThreadIsCritical = false;            // v1.0.38.04: Prevent the idle thread from being seen as uninterruptible.
  315.     g.AllowTimers = true;                  // v1.0.40.01: Similar to above.
  316.     g.AllowThreadToBeInterrupted = true;   // This is the primary line, the one above is not strictly necessary (just for maintainability).
  317.  
  318.     // Call it in this special mode to kick off the main event loop.
  319.     // Be sure to pass something >0 for the first param or it will
  320.     // return (and we never want this to return):
  321.     MsgSleep(SLEEP_INTERVAL, WAIT_FOR_MESSAGES);
  322.     return 0; // Never executed; avoids compiler warning.
  323. }
  324.